﻿using Business.UI.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Xml;
using System.Configuration;
using System.Windows.Media;

namespace WordGame
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        readonly int count = 0;
        public MainWindow()
        {
            InitializeComponent();
            wordlink.WordGroups = LoadXMl();
            count = wordlink.WordGroups.Count;
            string strPath = ConfigurationManager.AppSettings["level"];
            int.TryParse(strPath, out int lvel);
            wordlink.Level = lvel;
            Lveltext.Text = "级别:" + (wordlink.Level + 1);
            CoutText.Text = $"共{count + 1}级";
        }

        private void UP_Click(object sender, RoutedEventArgs e)
        {
            if (wordlink.Level < 1)
                MessageBox.Show("最初级啦!");
            else
            {
                wordlink.Level -= 1;
                Lveltext.Text = "级别:" + (wordlink.Level + 1);
            }
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {

            if (count <= wordlink.Level + 1)
                MessageBox.Show("最高级啦!");
            else
            {
                wordlink.Level += 1;
                Lveltext.Text = "级别:" + (wordlink.Level + 1);
            }
        }

        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            text.Text = "得  分:" + (count > 0 ? Math.Round((double)wordlink.RightCount / wordlink.WordGroups[wordlink.Level].Words.Count * 100, 0) : 0);
        }

        private void Resh_Click(object sender, RoutedEventArgs e)
        {
            wordlink.Refreash();
            text.Text = "得  分:";
        }

        /// <summary>
        /// 加载XML
        /// </summary>
        /// <returns></returns>
        List<WordGroup> LoadXMl()
        {
            List<WordGroup> wordGroups = new List<WordGroup>();
            XmlDocument xml = new XmlDocument();
            xml.Load(AppDomain.CurrentDomain.BaseDirectory + "config.xml");
            var xn = xml.SelectSingleNode("config").SelectNodes("wordgroup");
            for (int i = 0; i < xn.Count; i++)
            {
                if (Int32.TryParse((xn[i] as XmlElement).GetAttribute("time"), out int time))
                {
                    WordGroup wordGroup = new WordGroup()
                    {
                        Time = time,
                        Words = new List<Word>()
                    };
                    var tmp = xn[i].ChildNodes;
                    foreach (var item in tmp)
                    {
                        var tt = item as XmlElement;
                        var wordtmp = tt.InnerText.Replace("（", "(");
                        wordtmp = tt.InnerText.Replace("）", ")");
                        wordGroup.Words.Add(new Word(wordtmp));
                    }
                    wordGroups.Add(wordGroup);
                }
                else
                    MessageBox.Show("请检查配置文件。");
            }
            return wordGroups;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["level"].Value = Lveltext.Text.Replace("级别:", "");
            cfa.Save();
        }
    }

}
